package bb.lanxing.util.map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.model.LatLng;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.sport.BDOfflineManageActivity;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.fragment.BaiduMapFragment;
import bb.lanxing.fragment.base.BaseMapFragment;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.data.ResponseData;
import bb.lanxing.model.database.MapItem;
import bb.lanxing.model.database.ProPerms;
import bb.lanxing.mvp.model.SetupMapStyleModel;
import bb.lanxing.mvp.view.adapter.MapTileChooseAdapter;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.Log;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class MapTileChooseUtils {
    public static final String PRO_TILE = "http://map." + MyHttpClient.DOMAIN_BASE_PURE + "m/styles/dark-matter/{z}/{x}/{y}.png";
    public static final int PRO_TILE_TYPE = 999;
    private static final String TAG = "MapTileChooser";
    private int classicPosition;
    private boolean isCurPro;
    boolean isHideBaiDu;
    private boolean isWorkoutDetailPage;
    private View mapChangeBtn;
    private int[] mapClassicImage;
    private String[] mapClassicName;
    private BaseMapFragment mapFragment;
    private MapTileChangedListener mapTileChangedListener;
    private boolean needUpload;
    private View popupBg;
    private ViewGroup popupContainer;
    private MapTileChooseAdapter proChooseAdapter;
    private List<MapItem> proItems;
    private ProPerms proPerms;
    private int proPosition;
    private int tileType;
    private PopupWindow tileTypePopup;
    private String[] umengTileType;
    private String[] umengTileTypeV30;
    private long workoutId;

    public interface MapTileChangedListener {
        void afterChanged(BaseMapFragment baseMapFragment, int i);

        boolean beforeChange();

        void workoutMapIdChangge(long j, int i);
    }

    public MapTileChooseUtils(View view, ViewGroup viewGroup, BaseMapFragment baseMapFragment, int i, MapTileChangedListener mapTileChangedListener) {
        this(view, viewGroup, baseMapFragment, i, mapTileChangedListener, 0L, false, false);
    }

    public MapTileChooseUtils(View view, ViewGroup viewGroup, BaseMapFragment baseMapFragment, int i, MapTileChangedListener mapTileChangedListener, long j, boolean z, boolean z2) {
        this.workoutId = 0L;
        this.isWorkoutDetailPage = false;
        this.needUpload = false;
        this.umengTileType = new String[]{UmengEventConst.MAP_TILE_BD_NORMAL, UmengEventConst.MAP_TILE_BD_STELLITE};
        this.umengTileTypeV30 = new String[]{UmengEventConst.V30_MAP_TILE_BD_N, UmengEventConst.V30_MAP_TILE_BD_S};
        this.mapClassicName = new String[]{"百度标准", "百度卫星"};
        this.mapClassicImage = new int[]{R.drawable.ic_img_baidu_tile_normal, R.drawable.ic_img_baidu_satellite_tile};
        this.proPosition = 0;
        this.classicPosition = 0;
        this.isCurPro = false;
        this.mapChangeBtn = view;
        this.mapFragment = baseMapFragment;
        this.popupContainer = viewGroup;
        this.tileType = i;
        this.mapTileChangedListener = mapTileChangedListener;
        this.workoutId = j;
        this.isWorkoutDetailPage = z;
        this.needUpload = z2;
    }

    public void show() {
        FragmentActivity activity = this.mapFragment.getActivity();
        MobclickAgent.onEventValue(activity, "map_type", null, 1);
        int dp2px = DensityUtil.dp2px(activity, 330.0f);
        PopupWindow popupWindow = this.tileTypePopup;
        if (popupWindow == null || !popupWindow.isShowing()) {
            PopupWindow popupWindow2 = new PopupWindow(initTileChooseView(activity), dp2px, -2);
            this.tileTypePopup = popupWindow2;
            popupWindow2.setAnimationStyle(R.style.map_tile_type_popup_anim);
            this.tileTypePopup.setFocusable(true);
            this.tileTypePopup.setBackgroundDrawable(new BitmapDrawable());
            this.tileTypePopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    MapTileChooseUtils.this.tileTypePopup = null;
                    AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
                    alphaAnimation.setDuration(200L);
                    if (MapTileChooseUtils.this.popupBg != null) {
                        MapTileChooseUtils.this.popupBg.setVisibility(View.INVISIBLE);
                    }
                    alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (MapTileChooseUtils.this.popupBg != null) {
                                MapTileChooseUtils.this.popupBg.setVisibility(View.INVISIBLE);
                                MapTileChooseUtils.this.popupContainer.removeView(MapTileChooseUtils.this.popupBg);
                                MapTileChooseUtils.this.popupBg = null;
                            }
                        }
                    });
                    if (MapTileChooseUtils.this.popupBg != null) {
                        MapTileChooseUtils.this.popupBg.startAnimation(alphaAnimation);
                    }
                }
            });
        }
        View view = this.popupBg;
        if (view == null || view.getVisibility() != View.VISIBLE) {
            View view2 = new View(activity);
            this.popupBg = view2;
            view2.setBackgroundColor(Integer.MIN_VALUE);
            this.popupContainer.addView(this.popupBg);
        }
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation.setDuration(200L);
        this.popupBg.setVisibility(View.INVISIBLE);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
                if (MapTileChooseUtils.this.popupBg != null) {
                    MapTileChooseUtils.this.popupBg.setVisibility(View.VISIBLE);
                }
            }
        });
        this.popupBg.startAnimation(alphaAnimation);
        PopupWindow popupWindow3 = this.tileTypePopup;
        View view3 = this.mapChangeBtn;
        popupWindow3.showAsDropDown(view3, (-dp2px) + view3.getWidth(), -this.mapChangeBtn.getHeight());
    }

    private View initTileChooseView(final Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.multi_map_choose_utils, this.popupContainer, false);
        RecyclerView recyclerView = (RecyclerView) inflate.findViewById(R.id.rv_map);
        ((TextView) inflate.findViewById(R.id.map_tile_offline_data)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MapTileChooseUtils.this.tileTypePopup != null) {
                    MapTileChooseUtils.this.tileTypePopup.dismiss();
                }
                MobclickAgent.onEventValue(context, UmengEventConst.V30_MAP_OFFLINE, null, 1);
                MobclickAgent.onEventValue(context, "more_map", null, 1);
                context.startActivity(new Intent(context, BDOfflineManageActivity.class));
            }
        });
        final List<MapItem> initClassicData = initClassicData();
        final MapTileChooseAdapter mapTileChooseAdapter = new MapTileChooseAdapter(context, initClassicData, false);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        recyclerView.setAdapter(mapTileChooseAdapter);

        //TODO, hu, debug-Pro function
        ProPerms proPerms = ProPerms.getProPerms();
        this.proPerms = proPerms;
        if (proPerms != null) {
            this.proItems = proPerms.getMaps().getPro();
            this.proChooseAdapter = new MapTileChooseAdapter(context, this.proItems, true);
        }
        this.isCurPro = false;
        int curMapType = getCurMapType(this.tileType);
        this.classicPosition = curMapType;
        initClassicData.get(curMapType).setSelected(true);
        mapTileChooseAdapter.notifyDataSetChanged();
        recyclerView.scrollToPosition(getCurMapType(this.tileType));
        mapTileChooseAdapter.setOnItemClickListener(new MapTileChooseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                if (MapTileChooseUtils.this.isCurPro || MapTileChooseUtils.this.classicPosition != i) {
                    if (MapTileChooseUtils.this.proChooseAdapter != null && MapTileChooseUtils.this.tileType > 999) {
                        for (MapItem mapItem : MapTileChooseUtils.this.proItems) {
                            mapItem.setSelected(false);
                        }
                        MapTileChooseUtils.this.proChooseAdapter.notifyDataSetChanged();
                    } else {
                        MapTileChooseUtils mapTileChooseUtils = MapTileChooseUtils.this;
                        ((MapItem) initClassicData.get(mapTileChooseUtils.getCurMapType(mapTileChooseUtils.tileType))).setSelected(false);
                        for (MapItem mapItem2 : initClassicData) {
                            mapItem2.setSelected(false);
                        }
                        ((MapItem) initClassicData.get(i)).setSelected(true);
                        mapTileChooseAdapter.notifyDataSetChanged();
                    }
                    if (MapTileChooseUtils.this.tileTypePopup != null) {
                        MapTileChooseUtils.this.tileTypePopup.dismiss();
                    }
                    MapTileChooseUtils.this.switchMap(false, i, null, null);
                    MapTileChooseUtils.this.isCurPro = false;
                }
            }
        });
        MapTileChooseAdapter mapTileChooseAdapter2 = this.proChooseAdapter;
        if (mapTileChooseAdapter2 != null) {
            mapTileChooseAdapter2.setOnItemClickListener(new MapTileChooseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int i) {
                    if (!MapTileChooseUtils.this.isCurPro || MapTileChooseUtils.this.proPosition != i) {
                        if (MapTileChooseUtils.this.tileType > 999) {
                            for (MapItem mapItem : MapTileChooseUtils.this.proItems) {
                                mapItem.setSelected(false);
                            }
                            ((MapItem) MapTileChooseUtils.this.proItems.get(i)).setSelected(true);
                            MapTileChooseUtils.this.proChooseAdapter.notifyDataSetChanged();
                        } else {
                            for (MapItem mapItem2 : initClassicData) {
                                mapItem2.setSelected(false);
                            }
                            mapTileChooseAdapter.notifyDataSetChanged();
                        }
                        if (MapTileChooseUtils.this.tileTypePopup != null) {
                            MapTileChooseUtils.this.tileTypePopup.dismiss();
                        }
                        MapItem mapItem3 = (MapItem) MapTileChooseUtils.this.proItems.get(i);
                        MapTileChooseUtils.this.switchMap(true, mapItem3.getMapItemId(), mapItem3.getTileLinkv1(), mapItem3.getTrackColor());
                        Context context2 = context;
                        MobclickAgent.onEventValue(context2, UmengEventConst.MAP_PRO_TILE + mapItem3.getMapItemId(), null, 1);
                        MapTileChooseUtils.this.isCurPro = true;
                    }
                }
            });
        }
        return inflate;
    }

    public int getCurMapType(int i) {
        if (this.mapFragment instanceof BaiduMapFragment) {
            return (i != 0 && i == 1) ? 1 : 0;
        }
        if (i != 0) {
            if (i != 1) {
                if (i == 2) {
                    return 2;
                }
            }
        }
        return 1;
    }

    private List<MapItem> initClassicData() {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < this.mapClassicName.length; i++) {
            MapItem mapItem = new MapItem();
            mapItem.setName(this.mapClassicName[i]);
            mapItem.setMapImage(this.mapClassicImage[i]);
            mapItem.setMapType("classic");
            arrayList.add(mapItem);
        }
        return arrayList;
    }

    public void switchMap(boolean z, int i, String str, String str2) {
        LatLng center = this.mapFragment.getCenter();
        Log.i(TAG, "changeMap: ordinary " + center.toString());
        float zoomLevel = this.mapFragment.getZoomLevel();
        int locationMode = this.mapFragment.getLocationMode();
        int compassMargin = this.mapFragment.getCompassMargin();
        if (z) {
            LatLng baidu2Common = BiCiCoorConverter.baidu2Common(center);
            Log.i(TAG, "changeMap: osm " + baidu2Common.toString());
            this.tileType = i;
            MapTileChangedListener mapTileChangedListener2 = this.mapTileChangedListener;
            if (mapTileChangedListener2 != null) {
                mapTileChangedListener2.workoutMapIdChangge(this.workoutId, i);
            }
            if (this.workoutId <= 0 || !this.isWorkoutDetailPage) {
                SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TILE, i);
                SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TYPE, 2);
                if (i > 999) {
                    SharedManager.getInstance().setValue(MapConfigs.MAP_PRO_TILE, str);
                    SharedManager.getInstance().setValue(MapConfigs.MAP_PRO_LINE_COLOR, str2);
                }
            } else if (i > 999) {
                if (this.needUpload) {
                    new SetupMapStyleModel().uploadWorkoutMapId(this.workoutId, i).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ResponseData>() {
                        @Override
                        public void call(ResponseData responseData) {
                            if (responseData != null) {
                                responseData.getCode();
                            }
                        }
                    });
                }
            } else {
                if (i == 0 && this.needUpload) {
                    new SetupMapStyleModel().uploadWorkoutMapId(this.workoutId, i).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ResponseData>() {
                        @Override
                        public void call(ResponseData responseData) {
                            if (responseData != null) {
                                responseData.getCode();
                            }
                        }
                    });
                }
                SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TILE, i);
                SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TYPE, 2);
            }
//            this.mapFragment = OsmMapFragment.newInstance(baidu2Common.latitude, baidu2Common.longitude, false, (int) zoomLevel, locationMode, i, compassMargin);
        } else {
            BaseMapFragment baseMapFragment = this.mapFragment;
            if (baseMapFragment instanceof BaiduMapFragment) {
                if (this.tileType == i) {
                    return;
                }
                baseMapFragment.changeMapType(i);
                this.tileType = i;
                MapTileChangedListener mapTileChangedListener3 = this.mapTileChangedListener;
                if (mapTileChangedListener3 != null) {
                    mapTileChangedListener3.afterChanged(this.mapFragment, i);
                    this.mapTileChangedListener.workoutMapIdChangge(this.workoutId, i);
                }
                SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TILE, i);
                return;
            }
            LatLng common2Baidu = BiCiCoorConverter.common2Baidu(center);
            Log.i(TAG, "changeMap: baidu " + common2Baidu.toString());
            this.tileType = i;
            if (this.needUpload) {
                new SetupMapStyleModel().uploadWorkoutMapId(this.workoutId, i).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ResponseData>() {
                    @Override
                    public void call(ResponseData responseData) {
                        if (responseData != null) {
                            responseData.getCode();
                        }
                    }
                });
            }
            SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TILE, i);
            SharedManager.getInstance().setValue(MapConfigs.MULTI_MAP_TYPE, 1);
            this.mapFragment = BaiduMapFragment.newInstance(common2Baidu.latitude, common2Baidu.longitude, false, zoomLevel, locationMode, i, compassMargin);
        }
        MapTileChangedListener mapTileChangedListener4 = this.mapTileChangedListener;
        if (mapTileChangedListener4 != null) {
            mapTileChangedListener4.afterChanged(this.mapFragment, i);
            this.mapTileChangedListener.workoutMapIdChangge(this.workoutId, i);
        }
    }
}